package com.shangcheng.server.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangcheng.model.domain.OmsOrderReturnReason;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.mapper.OmsOrderReturnReasonMapper;
import com.shangcheng.server.service.OmsOrderReturnReasonService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/15 0015 zihao
 */
@Service
public class OmsOrderReturnReasonImpl extends ServiceImpl<OmsOrderReturnReasonMapper, OmsOrderReturnReason> implements OmsOrderReturnReasonService {
    
    //添加退货原因
    @Override
    public boolean create(OmsOrderReturnReason omsOrderReturnReason) {
        boolean save = this.save(omsOrderReturnReason);
        return save;
    }
    
    //批量删除退货原因
    @Override
    public boolean deletes(Long[] ids) {
        List<Long> objects = (List<Long>) Convert.toList(ids);
        boolean b = this.removeByIds(objects);
        return b;
    }
    
    //分页查询退货原因
    @Override
    public PageResult lists(Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnReason> page = new Page<>(pageNum,pageSize);
    
        LambdaQueryWrapper<OmsOrderReturnReason> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.orderByAsc(OmsOrderReturnReason::getSort);
        Page<OmsOrderReturnReason> pp = this.page(page,lambdaQueryWrapper);
        PageResult pageResult = new PageResult(Convert.toInt(pp.getCurrent()), Convert.toInt(pp.getSize()), Convert.toInt(pp.getTotal()), pp.getRecords());
        return pageResult;
    }
    
    //修改退货原因启用状态
    @Override
    public boolean status(Long[] ids, Integer status) {
        //先从ids中获取各个id,然后通过id查询出来符合条件的,
        boolean update = false;
        for (Long id : ids) {
            LambdaQueryWrapper<OmsOrderReturnReason> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OmsOrderReturnReason::getId,id);
            OmsOrderReturnReason omsOrderReturnReason = new OmsOrderReturnReason();
            omsOrderReturnReason.setStatus(status);
            // 根据符合条件的更新status是数据
            update = this.update(omsOrderReturnReason,lambdaQueryWrapper);
        }
        //返回
        return update;
    }
    
    //修改退货原因
    @Override
    public boolean updateByIdss(String id, OmsOrderReturnReason omsOrderReturnReason) {
        //先找id对应的数据
        LambdaQueryWrapper<OmsOrderReturnReason> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OmsOrderReturnReason::getId,id);
        //更新传入的数据
        boolean update = this.update(omsOrderReturnReason, lambdaQueryWrapper);
        return update;
    }
    
    //获取单个退货原因详情信息
    @Override
    public OmsOrderReturnReason getByIdd(Long id) {
        OmsOrderReturnReason byId = this.getById(id);
        return byId;
    }
}
